﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using Ordinaire.Commerce.Db;
using Ordinaire.Commerce.Data;

namespace Ordinaire.Commerce
{
    public class CommerceDataManager<TCContext> : ReferenceDataManager<TCContext>
        where TCContext : CContext
    {
        #region overrides

        /// <summary>
        /// Returns the preferred DataLoadOptions for current DataManager. This DataLoadOptions will be
        /// used by DataManager.Context when Context is instantiated.
        /// </summary>
        /// <returns>the DataLoadOptions set for current DataManager</returns>
        public override DataLoadOptions GetDataLoadOptions()
        {
            var loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Invoices>(d => d.InvoiceItems);
            return loadOptions;
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Returns purchase with the specified id.
        /// </summary>
        /// <param name="id">the purchase id</param>
        /// <returns>the purchase</returns>
        public TInvoices Get<TInvoices>(int id)
            where TInvoices : Invoices
        {
            return Get<TInvoices, int, string>(id);
        }

        /// <summary>
        /// Inserts new Purchases data with the specified date, organisation/person id, and serial number.
        /// </summary>
        /// <param name="date">the invoice date</param>
        /// <param name="organisationId">the organisation id where the goods was purchased from</param>
        /// <param name="personId">the person id whom the goods was purchased from</param>
        /// <param name="serialNo">optional purchase serial number</param>
        /// <returns>the created data</returns>
        public Invoices InsertInvoices<TInvoices>(DateTime date, int organisationId, int personId, string serialNo)
            where TInvoices : Invoices
        {
            TInvoices newData       = typeof(TInvoices).Instance<TInvoices>();
            newData.Date            = date;
            newData.SerialNo        = serialNo;
            newData.OrganisationId  = (organisationId <= 0) ? (int ?) null : organisationId;
            newData.PersonId        = (personId <= 0) ? (int ?) null :  personId;

            var results = Insert<TInvoices, int, string>(newData);
            return results[0];
        }

        #endregion
    }
}
